import  api  from '../utils/api.js'

class Pay {
  constructor() {}
  pay(user_id,order_id) {
    var that = this;

    wx.login({
      success: res => {

        http.request({
          url: api.gotoPreOrder,
          method: "POST",
          data: {
            uid: user_id,
            oid: order_id
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded',
          },
          success: function(res) {
            //后端返回的数据
            console.log('res支付', res)
            var data = res.data;
            that.doWxPay(data);
          },
          fail: function(err) {
            wx.showToast({
              title: "服务器异常，清稍候再试"
            });
          }
        });
      }
    });
  }
  doWxPay(param) {
    //小程序发起微信支付
    wx.requestPayment({
      timeStamp: param.data.timeStamp, //timeStamp是字符串类型的，秒
      nonceStr: param.data.nonceStr,
      package: param.data.package,
      signType: "md5",
      paySign: param.data.paySign,
      success: function(event) {
        wx.navigateTo({
          url: '/pages/paySuccess/paySuccess',
          success: function () {
            console.log('支付完成跳转！！！', event)
          }
        })
      },
      fail: function (error) {
        wx.showToast({
          title: '支付取消',
          icon: 'none',
          duration: 1500,
          mask: true,
          success: function () {
            console.log('支付取消跳转！！！', error)
            setTimeout(function () {
              wx.reLaunch({
                url: '/pages/myAllOrderList/myAllOrderList'
              })
            }, 2000);
          }
        });
      },
      complete: function() {
        // complete
      }
    });
  }
};

export {
  Pay
};